package com.intexh.quickandroid.module.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.intexh.quickandroid.R;
import com.intexh.quickandroid.base.BaseActivity;
import com.intexh.quickandroid.helper.UserHelper;
import com.intexh.quickandroid.module.mine.bean.VipLevelBean;
import com.intexh.quickandroid.net.Apis;
import com.intexh.quickandroid.net.NetworkManager;
import com.intexh.quickandroid.utils.GsonUtils;
import com.intexh.quickandroid.utils.ValidateUtils;
import com.intexh.quickandroid.utils.glide.GlideHelper;
import com.intexh.quickandroid.widget.easyadapter.BaseViewHolder;
import com.intexh.quickandroid.widget.easyadapter.RecyclerArrayAdapter;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LevelActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.title_menu_tv)
    TextView titleMenuTv;
    @BindView(R.id.avatar_iv)
    ImageView avatarIv;
    @BindView(R.id.userName_tv)
    TextView userNameTv;
    @BindView(R.id.level_name_tv)
    TextView levelNameTv;
    @BindView(R.id.seekBar)
    SeekBar seekBar;
    @BindView(R.id.min_level_tv)
    TextView minLevelTv;
    @BindView(R.id.max_level_tv)
    TextView maxLevelTv;
    @BindView(R.id.level_count_tv)
    TextView levelCountTv;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private RecyclerArrayAdapter<VipLevelBean> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level);
        ButterKnife.bind(this);
        titleTv.setText("会员等级");
        titleMenuTv.setText("积分明细");
        seekBar.setOnTouchListener((v, event) -> true);
        loadData();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new RecyclerArrayAdapter<VipLevelBean>(mContext) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_level_list,parent,false));
            }
            class ViewHolder extends BaseViewHolder<VipLevelBean>{
                TextView grade_expoints_tv,level_name_tv;
                public ViewHolder(View itemView) {
                    super(itemView);
                    level_name_tv = itemView.findViewById(R.id.level_name_tv);
                    grade_expoints_tv = itemView.findViewById(R.id.grade_expoints_tv);
                }

                @Override
                public void setData(VipLevelBean data) {
                    level_name_tv.setText(data.getLevel_name());
                    grade_expoints_tv.setText(data.getExppoints()+" 积分");
                }
            }
        });
    }

    private void loadData() {
        showProgress();
        NetworkManager.INSTANCE.post(Apis.grade_vip, new HashMap<>(), new NetworkManager.OnRequestCallBack() {
            @Override
            public void onOk(String response) {
                hideProgress();
                List<VipLevelBean> datas = GsonUtils.jsonToBeanList(response,new TypeToken<List<VipLevelBean>>(){}.getType());
                if(ValidateUtils.isValidate(datas)){
                    GlideHelper.INSTANCE.loadCircleImage(avatarIv, UserHelper.getUser().getMember_avatar());
                    userNameTv.setText(UserHelper.getUser().getUsername());
                    levelNameTv.setText(UserHelper.getUser().getMember_grade());
                    minLevelTv.setText(datas.get(0).getLevel_name()+"\n"+datas.get(0).getExppoints());
                    maxLevelTv.setText(datas.get(datas.size()-1).getLevel_name()+"\n"+datas.get(datas.size()-1).getExppoints());
                    seekBar.setMax(datas.get(datas.size()-1).getExppoints());
                    seekBar.setProgress(UserHelper.getUser().getMember_points());
                    levelCountTv.setText("分"+datas.size()+"个等级");
                    mAdapter.addAll(datas);
                }
            }

            @Override
            public void onError(String errorMessage) {
                hideProgress();
                showHintDialog(errorMessage);
            }
        });
    }

    @OnClick({R.id.back_iv, R.id.title_menu_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_menu_tv:
                //跳转积分明细 H5
//                WebViewActivity.startActivity(mContext, WebApis.pointslog_list);
                break;
        }
    }
}
